# -*- encoding:utf-8 -*-
# @Date  :2018-10-11
# @Author:daijin

"""
{
    "function": "知识管理", "权限系统","下载任务","安全事件","情报管理","网站管理","机构管理","用户管理","任务管理"......
    "type": "登录" "新增" "删除" "修改" "搜索" "下载" "编辑" "详情"
    "remark":"对操作的描述（如：删除的whois信息为：）"
    rem = {"域名":"www.hah.com",
        "注册商":"测试",
        "whois服务":"whois.paycenter.com.cn",
        "注册日期":"2018-05-20",
        "到期日期":"2018-10-11",
        "所有者":'',
        "注册国家":"中国",
        "注册省份":'None',
        "注册城市":"哈尔滨",
        "注册地址":'None'
        }
}
"""
# 示例如下：
"""
        g.cursor.execute(....)
        vel = g.cursor.fetchall()
        g.conn.commit()
        根据自己的字段拼接转换成字典格式如：
        rem = {"域名": "%s" % vel[0]['domain'],
               "注册商": "%s" % vel[0]['registrar'],
               "whois服务": "%s" % vel[0]['whois_server'],
               "注册日期": "%s" % vel[0]['creation_date'],
               "到期日期": "%s" % vel[0]['expiration_date'],
               "所有者": "%s" % vel[0]['name'],
               "注册国家": "%s" % vel[0]['country'],
               "注册省份": "%s" % vel[0]['province'],
               "注册城市": "%s" % vel[0]['city'],
               "注册地址": "%s" % vel[0]['address']}
        auth.logsync(_currUser, {"function": "知识管理", "type": "删除", "remark": "删除whois信息:" + recodelog(rem)})
        return json.dumps({"status": "success", "msg": "删除成功"})
    except Exception, e:
        return json.dumps({"status": "except", 'msg': '删除失败', 'warn': str(e)}))
"""


# 将list中数据连接为key：value，key：value...形式
def joinlist(data):
    key_str = ""
    for i in range(len(data)):
        if i == 0:
            key_str = key_str + data[i]
        else:
            key_str = key_str + ', ' + data[i]
    return key_str


# 过滤掉为空的字段
def recodelog(remark):
    listans = []
    for key in remark:
        if remark[key] == 'None' or remark[key] == '' or remark[key] is None or remark[key] == ' ':
            continue
        else:
            listans.append(key + ':' + str(remark[key]))
    return joinlist(listans)
